##=============================================================================
## Appendix: Table 3
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "mice", "ggeffects", "ordinal", "sjPlot", "lmtest",
              "ggeffects", "cobalt", "MatchIt")

ipak(packages)

##---------
# Load data
#setwd("~/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

test1 <- dat %>% 
  select(id, wave, save_lives, survival_mindset, sex, age, edu, job, finances, 
         home_lang, ethnic, rus_ethnic, displaced) %>% 
  group_by(wave) %>% 
  filter(is.na(save_lives) & is.na(survival_mindset)) %>% 
  mutate(var = "Missing Both")

test2 <- dat %>% 
  select(id, wave, save_lives, survival_mindset, sex, age, edu, job, finances, 
         home_lang, ethnic, rus_ethnic, displaced) %>% 
  group_by(wave) %>% 
  filter(!is.na(save_lives) & !is.na(survival_mindset)) %>% 
  mutate(var = "Complete Cases")

test3 <- dat %>% 
  select(id, wave, save_lives, survival_mindset, sex, age, edu, job, finances, 
         home_lang, ethnic, rus_ethnic, displaced) %>% 
  group_by(wave) %>% 
  filter(!is.na(save_lives) & is.na(survival_mindset)) %>% 
  mutate(var = "Missing `Survival Mindset`")

test4 <- dat %>% 
  select(id, wave, save_lives, survival_mindset, sex, age, edu, job, finances, 
         home_lang, ethnic, rus_ethnic, displaced) %>% 
  group_by(wave) %>% 
  filter(is.na(save_lives) & !is.na(survival_mindset)) %>% 
  mutate(var = "Missing `Save Lives`")

dat3 <- test1 %>% 
  bind_rows(test2) %>% 
  bind_rows(test3) %>% 
  bind_rows(test4) %>% 
  mutate(wave = case_when(
    wave == 0 ~ "July 2022",
    wave == 1 ~ "February 2023")) %>% 
  mutate(wave = factor(wave)) %>% 
  mutate(var = factor(var)) %>% 
  mutate(hs_edu = ifelse(edu > 4, 1, 0),
         poor = ifelse(finances < 3, 1, 0),
         ukr_home_lang = ifelse(home_lang == 1, 1, 0)) %>% 
  ungroup() 

rm(test1, test2, test3, test4)

##------------------------------
# Descriptive stats by subsample

dat3 %>%
  group_by(wave) %>% 
  summarise(across(sex:displaced, list(
    mean = ~ mean(.x, na.rm = TRUE),
    n = ~ sum(!is.na(.x)),
    se = ~ sd(.x)/sqrt(length(.x))),
    .names = "{col}_{fn}")) 

# complete cases
dat3 %>%
  filter(var == "Complete Cases") %>% 
  summarize(across(c(sex, age, hs_edu, poor, rus_ethnic, displaced, ukr_home_lang), 
                   list(mean = ~ mean(.x, na.rm = TRUE), 
                        std = ~ sd(.x, na.rm = TRUE)),
                   .names = "{col}_{fn}")) %>% 
  pivot_longer(cols = sex_mean:ukr_home_lang_std, names_to = "statistic", 
               values_to = "value")

# save lives
dat3 %>%
  filter(var == "Missing `Save Lives`") %>% 
  summarize(across(c(sex, age, hs_edu, poor, rus_ethnic, displaced, ukr_home_lang), 
                   list(mean = ~ mean(.x, na.rm = TRUE), 
                        std = ~ sd(.x, na.rm = TRUE)),
                   .names = "{col}_{fn}")) %>% 
  pivot_longer(cols = sex_mean:ukr_home_lang_std, names_to = "statistic", 
               values_to = "value")

# survival mindset
dat3 %>%
  filter(var == "Missing `Survival Mindset`") %>% 
  summarize(across(c(sex, age, hs_edu, poor, rus_ethnic, displaced, ukr_home_lang), 
                   list(mean = ~ mean(.x, na.rm = TRUE), 
                        std = ~ sd(.x, na.rm = TRUE)),
                   .names = "{col}_{fn}")) %>% 
  pivot_longer(cols = sex_mean:ukr_home_lang_std, names_to = "statistic", 
               values_to = "value")

# both
dat3 %>%
  filter(var == "Missing Both") %>% 
  summarize(across(c(sex, age, hs_edu, poor, rus_ethnic, displaced, ukr_home_lang), 
                   list(mean = ~ mean(.x, na.rm = TRUE), 
                        std = ~ sd(.x, na.rm = TRUE)),
                   .names = "{col}_{fn}")) %>% 
  pivot_longer(cols = sex_mean:ukr_home_lang_std, names_to = "statistic", 
               values_to = "value")

##-----------------------------------
# T-tests with Bonferroni corrections
tidy(pairwise.t.test(dat3$sex, dat3$var, p.adj = "bonf"))
tidy(pairwise.t.test(dat3$age, dat3$var, p.adj = "bonf"))
tidy(pairwise.t.test(dat3$hs_edu, dat3$var, p.adj = "bonf"))
tidy(pairwise.t.test(dat3$poor, dat3$var, p.adj = "bonf"))
tidy(pairwise.t.test(dat3$rus_ethnic, dat3$var, p.adj = "bonf"))
tidy(pairwise.t.test(dat3$ukr_home_lang, dat3$var, p.adj = "bonf"))
tidy(pairwise.t.test(dat3$displaced, dat3$var, p.adj = "bonf"))

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================